from PySide6.QtWidgets import  (QVBoxLayout, QWidget,QFrame)
from pathlib import Path
from qfluentwidgets import (TextBrowser)
class HtmlViewer(QWidget):
    def __init__(self, html_file_path):
        super().__init__()
        
        # 创建一个 QTextBrowser 实例
        self.text_browser = TextBrowser(self)
        
        # 设置布局
        layout = QVBoxLayout()
        layout.addWidget(self.text_browser)
        self.setLayout(layout)
        
        # 读取并显示HTML文件内容
        self.load_html(html_file_path)

    def load_html(self, file_path):      
        if file_path.exists() and file_path.is_file():
            with open(file_path, 'r', encoding='utf-8') as f:
                html_content = f.read()
                # self.text_browser.setHtml(html_content)
                self.text_browser.setMarkdown(html_content)
        else:
            print(f"文件 {file_path} 不存在")

class ReadMe(QFrame):

    def __init__(self, text: str, parent=None):
        super().__init__(parent=parent)
        self.setObjectName(text.replace(' ', '-'))
        main_layout = QVBoxLayout()
    
        # 指定本地HTML文件路径
        html_file = 'help_01.md'
        current_dir = Path(__file__).parent
        # 拼接文件路径
        html_file_path = current_dir / html_file
        
        # 创建并显示窗口
        viewer = HtmlViewer(html_file_path)
        main_layout.addWidget(viewer)
        self.setLayout(main_layout)
